﻿using System.Collections.Generic;
using System.Linq;

using VirtualTournaments.DataAccess.EntityManagers;
using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.EntityManagers
{
	internal sealed class TournamentsManager : ITournamentsManager
	{
		#region Singleton implementation

		private static TournamentsManager _instance = null;
		private static object _instanceLocker = new object();

		private TournamentsManager() { }

		public static TournamentsManager Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new TournamentsManager();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		#region ITournamentsManager Members

		public IEnumerable<Business.Tournaments.Tournament> GetActives()
		{
			List<Business.Tournaments.Tournament> result = new List<Business.Tournaments.Tournament>();
			result.AddRange(AllAgainstAllTournamentsDataMapper.Instance.GetList(tournament => tournament.IsActive).Cast<Business.Tournaments.Tournament>());
			//TODO: implementar
			//result.AddRange(PlayOffTournamentsDataMapper.Instance.GetList(tournament => tournament.IsActive).Cast<Business.Tournaments.Tournament>());
			return result;
		}

		public IEnumerable<Business.Tournaments.Tournament> GetBySimilarName(string name)
		{
			List<Business.Tournaments.Tournament> result = new List<Business.Tournaments.Tournament>();
			result.AddRange(AllAgainstAllTournamentsDataMapper.Instance.GetList(tournament => tournament.Name.ToLower().Contains(name.ToLower())).Cast<Business.Tournaments.Tournament>());
			//TODO: implementar
			//result.AddRange(PlayOffTournamentsDataMapper.Instance.GetList(tournament => tournament.Name.ToLower().Contains(name.ToLower())).Cast<Business.Tournaments.Tournament>());
			return result;
		}

		public Business.Tournaments.Tournament GetByExactName(string name)
		{
			Business.Tournaments.Tournament result = null;
			result = AllAgainstAllTournamentsDataMapper.Instance.GetOneAsBusinessObject(tournament => tournament.Name.ToLower() == name.ToLower());
			if (result != null)
				return result;
			//result = PlayOffTournamentsDataMapper.Instance.GetOneAsBusinessObject(tournament => tournament.Name.ToLower() == name.ToLower());
			//if (result != null)
			//    return result;
			return null;
		}

		#endregion

		#region IEntityManager<ITournament> Members

		public IEnumerable<Business.Tournaments.Tournament> GetAll()
		{
			List<Business.Tournaments.Tournament> result = new List<Business.Tournaments.Tournament>();
			result.AddRange(AllAgainstAllTournamentsDataMapper.Instance.GetList().Cast<Business.Tournaments.Tournament>());
			//TODO: implementar
			//result.AddRange(PlayOffTournamentsDataMapper.Instance.GetList().Cast<Business.Tournaments.Tournament>());
			return result;
		}

		public void Save(Business.Tournaments.Tournament entity)
		{
			if (entity is Business.Tournaments.AllAgainstAllTournament)
			{
				AllAgainstAllTournamentsDataMapper.Instance.Save((Business.Tournaments.AllAgainstAllTournament)entity);
				AllAgainstAllTournamentsDataMapper.Instance.GetDataContext().SubmitChanges();
			}
			else
			{
				throw new System.NotImplementedException();
			}
		}

		public void Delete(Business.Tournaments.Tournament entity)
		{
			if (entity is Business.Tournaments.AllAgainstAllTournament)
			{
				AllAgainstAllTournamentsDataMapper.Instance.Delete((Business.Tournaments.AllAgainstAllTournament)entity);
			}
			else
			{
				throw new System.NotImplementedException();
			}
		}

		#endregion
	}
}
